package me.nibo.tree;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import me.nibo.tree.utils.Node;
import me.nibo.tree.utils.adapter.TreeListViewAdapter;

/**
 * @author nibo
 */
public class SimpleTreeListViewAdapter<T> extends TreeListViewAdapter<T> {

    public SimpleTreeListViewAdapter(Context context, List<T> datas, int defaultExpandLevel,
                                     ListView listView) throws IllegalAccessException {
        super(context, datas, defaultExpandLevel, listView);
    }

    @Override
    public View getConvertView(Node node, int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.item_list, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.mImageView = (ImageView) convertView.findViewById(R.id.iv_icon);
            viewHolder.mTextView = (TextView) convertView.findViewById(R.id.tv_label);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (node.getIcon() == -1) {
            viewHolder.mImageView.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.mImageView.setImageResource(node.getIcon());
            viewHolder.mImageView.setVisibility(View.VISIBLE);
        }

        viewHolder.mTextView.setText(node.getName());
        return convertView;
    }

    private class ViewHolder {
        ImageView mImageView;
        TextView mTextView;
    }
}
